<?php
$status = 0; 
$error = 0; 
$dataid = 0; 
$desc = "";
/*
 * status :
 * 	0 = sukses
 * 	2 = error
 * error : 
 * 	0 = no error
 * 	1 = ip error
 * 	2 = parameter npsn tidak ada
 *  3 = parameter nama sekolah tidak ada 
 *  4 = panjang nama sekolah tidak sesuai
 *  5 = nama sekolah mengandung karakter yang tidak dijinkan
 *  6 = nama sekolah sudah ada pada DB
 *  7 = panjang npsn tidak sesuai
 *  8 = npsn mengandung karakter yang tidak dijinkan
 *  9 = group dengan npsn sudah ada di DB
 *  10 = no parameter
 * dataid :
 * 	0 = always 0
 * desc :
 * 	sesuai error code 
 */

if($_GET){
	
	$json=$_GET["json"];
	$json =  str_replace("\\", "", $json);
	$data=json_decode($json,true);
	$groupname= trim($data['npsn']);//NPSN
	$title=trim($data['nama sekolah']);//NAMA SEKOLAH
	$ip=trim($data['ip']);//ip

	
	if($ip!='192.168.1.121'){
		$status = 2;
		$error = 1;
		$desc = "ip request error";
		$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
		print_r (json_encode($respond));
		return;
		
	}else{
		$description=$title;
		$is_public=TRUE;
			if(!$groupname){
				$status = 2;
				$error = 2;
				$desc = "parameter npsn tidak ada";
				$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
				print_r (json_encode($respond));
				return;
			}if(!$title){
				$status = 2;
				$error = 3;
				$desc = "parameter nama sekolah tidak ada";
				$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
				print_r (json_encode($respond));
				return;
			}
			$error=0;
			if( mb_strlen($title)<3 || mb_strlen($title)>50 ){
				$status = 2;
				$error = 4;
				$desc = "panjang nama sekolah tidak sesuai";
				$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
				print_r (json_encode($respond));
				return;
			}elseif( preg_match('/[^a-zа-я0-9\-\.\s]/iu', $title) ){
				$status = 2;
				$error = 5;
				$desc = "nama sekolah mengandung karakter yang tidak dijinkan";
				$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
				print_r (json_encode($respond));
				return;
			}else{
				$db2->query('SELECT id FROM groups WHERE (groupname="'.$title.'" OR title="'.$title.'") LIMIT 1');
				if( $db2->num_rows() > 0 ) {
					$status = 2;
					$error = 6;
					$desc = "nama sekolah sudah ada pada DB";
					$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
					print_r (json_encode($respond));
					return;
				}
			}
			if($error==0 ) {
				if( mb_strlen($groupname)<3 || mb_strlen($groupname)>30 ) {
					$status = 2;
					$error = 7;
					$desc = "panjang npsn tidak sesuai";
					$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
					print_r (json_encode($respond));
					return;
				}elseif( ! preg_match('/^[a-z0-9\-\_]{3,30}$/iu', $groupname) ) {
					$status = 2;
					$error = 8;
					$desc = "npsn mengandung karakter yang tidak dijinkan";
					$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
					print_r (json_encode($respond));
					return;
				}else {
					$db2->query('SELECT id FROM groups WHERE (groupname="'.$groupname.'" OR title="'.$groupname.'") LIMIT 1');
					if( $db2->num_rows() > 0 ) {
						$status = 2;
						$error = 9;
						$desc = "group dengan npsn sudah ada di DB";
						$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
						print_r (json_encode($respond));
						return;
					}else{
					}
				}
			}
			if($error==0 ) {
				$db2->query('INSERT INTO groups SET groupname="'.$groupname.'", title="'.$title.'", about_me="'.$description.'", is_public="1" ');	
				$group_id=$this->db2->fetch_field('SELECT id FROM groups WHERE (groupname="'.$groupname.'")');
				$this->db2->query('INSERT INTO groups_admins SET group_id="'.$group_id.'", user_id="1" ');				$this->db2->query('INSERT INTO groups_followed SET user_id="1", group_id="'.$group_id.'", date="'.time().'"');;
				$this->db2->query('UPDATE groups SET num_followers="1" WHERE (groupname="'.$groupname.'")');
				$status = 0;
				$error = 0;
				$desc = "group create berhasil";
				$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
				print_r (json_encode($respond));
				return;
			}
	}
	
}
else{
	if(!$_GET){
		$status = 2;
		$error = 10;
		$desc = "no parameter";
		$respond = array('status'=>$status,'error'=>$error,'dataid'=>$dataid,'desc'=>$desc);
		print_r (json_encode($respond));
		return;
	}
}
?>
